home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / Pascal / MicroAnimationDemo / MicroAnimationDemo.p < prev    next >
Text File  |  1995-06-15  |  4KB  |  131 lines

  1. {MicroAnimationDemo by Ingemar Ragnemalm}
  2.  
  3. {Was OffscreenToys too elaborate for you? I know, it keeps some extra information to avoid}
  4. {redrawing the whole window, and the collision handling is pretty fancy. This is stipped down}
  5. {to the limit!}
  6.  
  7. {This was really intended for the Mac game programming book, as a first intro to offscren}
  8. {animation, but they found someone else to make that chapter, so what the heck, then I'll}
  9. {let it be a free little demo (and won't bother making a C version).}
  10.  
  11. {So, if it is THIS simple to make sprite animation, what's the point with SAT or even}
  12. {OffscreenToys? I'll tell you: Speed, flexibility, compatibility, expandability.}
  13.  
  14. {PlotCIcon is not the fastest routine around. Actually, it is one of the slower. Check out}
  15. {OffScreenToysBoost to learn how to speed it up (replacing PlotCIcon with CopyBits).}
  16. {Also, it can be speeded up by not erasing the entire offscreen and by not copying all of it}
  17. {every frame. However, more speed takes more code.}
  18.  
  19. program MicroAnimationDemo;
  20.     uses
  21. {$IFC UNDEFINED THINK_PASCAL}
  22.         Types, QuickDraw, Fonts, Events, Packages, Menus, Dialogs, Windows,{}
  23.         OSUtils, ToolUtils, Resources, Icons, 
  24. {$ENDC}
  25.         QDOffScreen;
  26.     const
  27.         kMaxObject = 5;
  28.     var
  29.         cicn: array[0..kMaxObject] of CIconHandle;
  30.         pos: array[0..kMaxObject] of Rect;
  31.         speed: array[0..kMaxObject] of Point;
  32.         wind: WindowPtr;
  33.         offScreen: GrafPtr;
  34.         backPat: PixPatHandle;
  35.         i: Integer;
  36.         startTicks: Longint;
  37.  
  38. (* Standard inits *)
  39.     procedure InitToolbox;
  40.     begin
  41. {$IFC UNDEFINED THINK_PASCAL}
  42.         InitGraf(@qd.thePort);
  43.         InitFonts;
  44.         FlushEvents(everyEvent, 0);
  45.         InitWindows;
  46.         InitMenus;
  47.         TEInit;
  48.         InitDialogs(nil);
  49.         InitCursor;
  50. {$ENDC}
  51.     end; (*InitToolbox*)
  52.  
  53. begin
  54.     InitToolbox;
  55.  
  56. {Is Color QD and 32-bit QD around?}
  57.     if NGetTrapAddress($A89F, ToolTrap) = NGetTrapAddress($AA1E, ToolTrap) then {Color QD?}
  58.         halt;
  59.     if NGetTrapAddress($A89F, ToolTrap) = NGetTrapAddress($AB1D, ToolTrap) then {32-bit QD?}
  60.         halt;
  61.  
  62. {Seed the random number generator}
  63. {$IFC UNDEFINED THINK_PASCAL}
  64.     qd.randSeed := TickCount;
  65. {$ELSEC}
  66.     randSeed := TickCount;
  67. {$ENDC}
  68.  
  69. {Create window from resource}
  70.     wind := GetNewCWindow(128, nil, WindowPtr(-1));
  71.     SetPort(wind);
  72.  
  73. {Make GWorld with default depth and CLUT}
  74. {$IFC UNDEFINED THINK_PASCAL}
  75.     if noErr <> NewGWorld(GWorldPtr(offScreen), 0, wind^.portRect, nil, nil, pixelsLocked) then
  76. {$ELSEC}
  77.         if noErr <> NewGWorld(GWorldPtr(offScreen), 0, wind^.portRect, nil, nil, [pixelsLocked]) then
  78. {$ENDC}
  79.             halt;
  80. {We lock the offscreen pixmap so we can CopyBits and PlotCIcon to it.}
  81.     if LockPixels(CGrafPtr(offScreen)^.portPixMap) then
  82.         ;
  83.  
  84.     SetGWorld(GWorldPtr(offScreen), nil);
  85.  
  86. {Get a pattern and set it to the background pattern for offScreen}
  87.     backPat := GetPixPat(128);
  88.     BackPixPat(backPat);
  89.  
  90. {Set up all objects, loading the 'cicn' resource, and setting its position and speed}
  91.     for i := 0 to kMaxObject do
  92.         begin
  93.             cicn[i] := GetCIcon(128 + i);
  94.             pos[i] := cicn[i]^^.iconMask.bounds;
  95.             OffsetRect(pos[i], abs(Random) mod wind^.portRect.right, abs(Random) mod wind^.portRect.bottom);
  96.             speed[i].h := Random mod 8;
  97.             speed[i].v := Random mod 8;
  98.         end;
  99.  
  100. {*** The animation loop: ***}
  101.  
  102.     while not Button do
  103.         begin
  104.             startTicks := TickCount;
  105. {Erase the offscreen, resetting it to the background image/pattern}
  106.             SetGWorld(GWorldPtr(offScreen), nil);
  107.             EraseRect(wind^.portRect);
  108. {For all objects, move, draw off-screen and do border checks}
  109.             for i := 0 to kMaxObject do
  110.                 begin
  111.                     OffSetRect(pos[i], speed[i].h, speed[i].v);
  112.                     PlotCIcon(pos[i], cicn[i]);
  113.                     if pos[i].top < 0 then
  114.                         speed[i].v := abs(speed[i].v);
  115.                     if pos[i].left < 0 then
  116.                         speed[i].h := abs(speed[i].h);
  117.                     if pos[i].bottom > wind^.portRect.bottom then
  118.                         speed[i].v := -abs(speed[i].v);
  119.                     if pos[i].right > wind^.portRect.right then
  120.                         speed[i].h := -abs(speed[i].h);
  121.                 end;
  122. {Copy from offscreen to screen}
  123.             SetGWorld(GWorldPtr(wind), GetMainDevice);
  124.             CopyBits(offScreen^.portBits, wind^.portBits, wind^.portRect, wind^.portRect, srcCopy, nil);
  125. {Wait until at least one tick has passed}
  126.             while TickCount = startTicks do
  127.                 ;
  128.         end;
  129. {Set back the device before we quit}
  130.     SetGWorld(GWorldPtr(wind), GetMainDevice);
  131. end. {MicroAnimationDemo}